<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\controller\api\energy;

use think\App;
use app\controller\api\Base;
use app\validate\energy\EnergySharedPlanValidate;
use app\common\repositories\energy\EnergyMeterRepository;
use app\common\repositories\energy\EnergySharedLogsRepository;
use app\common\repositories\energy\EnergySharedRoomRepository;
use app\common\repositories\energy\EnergySharedPlanRepository;


class EnergySharedRoom extends Base
{
    protected $repository;

    public function __construct(App $app, EnergySharedRoomRepository $repository)
    {
        parent::__construct($app);
        $this->repository = $repository;
    }

    public function list()
    {
        $where = $this->request->param([
            'keywords' => '',
            'plan_id' => '',
            'is_settle' => '',
        ]);
        [$page, $limit] = $this->getPage();
        $where['add_uid'] = $this->request->userId();
        $res = $this->repository->getApiList($where, $page, $limit, $this->request->propertyId);
        return $this->success($res);
    }

    /**
     * 未绑定房间列表
     * @param int $id
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     *
     * @author Amos
     * 2024-05-19 12:32
     */
    public function roomList(int $id)
    {
        /** @var EnergySharedPlanRepository $energySharedPlanRepository */
        $energySharedPlanRepository = app()->make(EnergySharedPlanRepository::class);
        $info = $energySharedPlanRepository->get($id);
        if (!$info) {
            return app('api_return')->error('数据不存在');
        }
        if (!$energySharedPlanRepository->existsByUserId($id, $this->request->userId())) {
            return $this->error('权限不足');
        }
        $where['plan_id'] = $info['id'];
        $where['property_id'] = $info['property_id'];
        $res = $this->repository->getApiNoRoomList($where);
        return $this->success($res);
    }

    /**
     *  房间分摊日志
     * @param int $id
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     *
     * @author Amos
     * 2024-05-19 12:44
     */
    public function roomLogs(int $id, EnergySharedLogsRepository $logRepository)
    {
        $info = $this->repository->get($id);
        if (!$info) {
            return app('api_return')->error('数据不存在');
        }
        if (!$this->repository->existsByUserId($id, $this->request->userId())) {
            return $this->error('权限不足');
        }
        [$page, $limit] = $this->getPage();
        $where['shared_room_id'] = $info['id'];
        $where['shared_plan_id'] = $info['plan_id'];
        $res = $logRepository->getItemsList($where, $page, $limit, $this->request->propertyId);
        return $this->success($res);
    }


    public function add()
    {
        $param = $this->request->param([
            'plan_id' => '',
            'room' => [],
        ]);
        /** @var EnergySharedPlanRepository $energySharedPlanRepository */
        $energySharedPlanRepository = app()->make(EnergySharedPlanRepository::class);
        $sharedPlanInfo = $energySharedPlanRepository->get($param['plan_id']);
        if (!$sharedPlanInfo) {
            return $this->error('方案不存在');
        }
        foreach ($param['room'] as $k => $v) {
            if (!isset($param['room'])) {
                unset($param['room'][$k]);
            }
        }
        $param['room'] = array_filter($param['room']);
        $this->repository->schemeConfig($sharedPlanInfo, $param, $this->request->userId());
        return app('api_return')->success('添加成功');
    }


    public function edit(int $id)
    {
        $param = $this->request->param([
            'scale' => '',
            'remark' => '',
            'use_num_z' => '',
        ]);
        $info = $this->repository->get($id);
        if (!$this->repository->existsByUserId($id, $this->request->userId())) {
            return $this->error('权限不足');
        }
        if (!$info) {
            return app('api_return')->error('数据不存在');
        }
        $this->repository->editInfo($info, $param, $this->request->userId());
        return app('api_return')->success('更新成功');
    }


    public function delete(int $id)
    {
        $info = $this->repository->get($id);
        if (!$info) {
            return app('api_return')->error('数据不存在');
        }
        if (!$this->repository->existsByUserId($id, $this->request->userId())) {
            return $this->error('权限不足');
        }
        $res = $this->repository->batchDelete([$id], $this->request->userId());
        if ($res) {
            return $this->successText('删除成功');
        } else {
            return $this->error('删除失败');
        }
    }

}